extends Node

# 基础语法，与python非常类似，看看就好

##### Numbers
# Integers
var a = 1
var b = -23
var c = 0
var d = 0xA1Fe316 # hexadecimal

# Floats
var x = 1.0
var y = -43.01
var z = 1.3e6

# Constant
const THE_ANSWER = 42

var score = 5
var new_score

# A function that may be called to add points to score
func add_to_score(points):
	# points is a variable passed from outside
	# it has local scope in this indented code block
	score = score + points

func _ready() -> void:
	print('program demo ready!!')
	
	print(a)
	print(b)
	print(c)
	print(d)
	
	# 动态类型变量
	var _v_score = 0
	var _v_remaining_fuel = 99.9
	var _v_paused = false
	var _v_player_name = ""
	var _v_selected_weapon
	var _v_starting_grid_position
	
	_v_score = "false"
	
	# 静态类型变量
	# Method 1
	var _s_score: int = 0
	var _s_remaining_fuel: float = 99.9
	var _s_paused: bool = false
	var _s_player_name: String = ""

	# Method 2 (inferring the type)
	var _s_my_int := 8
	var _s_size := 32.6
	var _s_running := true
	var _s_name := ""
	
	# 如果变量为静态类型变量就不能变更类型，这里会报错
	# s_name = 100
	
	print("score before:" + String.num(self.score))
	add_to_score(1)
	print("score after:" + String.num(self.score))
	
	print("get_sum(1,2)=" + String.num(self.get_sum(1,2)))
	
	# 逻辑判断
	var paid = false
	var strength = 9.9 if paid else 1.0
	print("Strength = ", strength)
	
	self.conditional_demo()
	
	# 循环的写法
	self.loop_demo()
	
	# 数组
	self.array_demo()
	
	# 字典
	self.dict_demo()
	
var count: int = 0
func _process(delta: float):
	#self.input_process(delta)
	return delta
	
func input_process(delta: float):
	if (Input.is_action_just_pressed("ui_accept")):
		# Print to Output window
		print("Key down")
	if (Input.is_action_just_released("ui_accept")):
		print("Key up")
	if (Input.is_action_pressed("ui_up")):
		count += 1
		print(count)
	if (Input.is_key_pressed(KEY_F)):
		var fps = 1.0 / delta
		print("FPS: %d" % fps)
	if (Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT)):
		print("Left mouse button pressed!")
	if (Input.get_mouse_button_mask() == 0x03):
		print("Left and right mouse buttons pressed!")
	if (Input.is_key_pressed(KEY_ESCAPE)):
		get_tree().quit()
	
# 函数定义------
# This function has no return value
func add(a, b):
	print(a + b)

# This function returns a value
func get_sum(a, b):
	return a + b

# This function will only accept integer arguments
func add_ints(a: int, b: int):
	return a + b

# Generate an error if the return value is not an int
func times_2(n) -> int:
	return 2 * n

# This function modifies an object that is passed by reference
func move_x(node: Node2D, dx = 1.5):
	node.position.x += dx

func conditional_demo():
	var n = 6
	
	# Inline 'if' statement
	if n == 6: print("n is equal to six")
	
	n = 4
	# Regular 'if' statement 
	if n == 4:
		print("n is equal to four")
	
	# 'else/if' statement
	if n == 6:
		print("n is equal to six")
	else:
		print("n is not equal to six")
	
	# Messy indented 'else/if' statement
	if n == 6:
		print("n is equal to six")
	else:
		if n < 6:
			print("n is less than six")
		else:
			print("n is greater than six")
	
	n = 8
	# Tidier 'else/if' statement using 'elif'
	if n == 6:
		print("n is equal to six")
	elif n < 6:
		print("n is less than six")
	else:
		print("n is greater than six")
		
func loop_demo():
	# loop for n = 0 to 7
	for n in 8:
		print(n)

	# Using range
	for n in range(8):
		print(n)

	# loop for n = 10 to 12
	for n in range(10,13):
		print(n)

	# count down from 10 to 1
	for n in range(10,0,-1):
		print(n)

	# loop for n = 2,4,6,8 in steps of 2
	for n in range(2,9,2):
		print(n)

	# Iterate over string (array of characters)
	for ch in "Hello":
		print(ch)

	# Iterate over an array of numbers
	for x in [3,6,8,9]:
		print(x)

	# Iterate over items of a dictionary
	var dict = { "x": 1, "y": 2, "z": 3 }
	for key in dict:
		# Insert the key and value into a text string
		print("index: %s, value: %d" % [key, dict[key]])

	# Using continue and break statements
	for n in 9:
		# Skip numbers below 3
		if n < 3:
			continue
		# Break out of the loop for numbers above 5
		if n > 5:
			break
		print(n)
		
	var fuel = 1000
	var speed = 0

	while fuel > 0:
		speed += 0.12
		fuel -= 1

	print("Top speed = ", speed)

func array_demo():
	# Ways to create an array instance
	var a = Array()
	var b = []
	var c = ["a","b","c"]
	
	# Add some items to array 'a'
	a.append("Item 1")
	a.append("Item 2")
	
	# Pass array by reference to a function
	change(a)
	# Confirm that changes were made
	print(a[0])
	
	# Print the size of array 'b'
	print(b.size())
	
	# Shuffle the values of array 'c'
	c.shuffle() # This function doesn't return a value
	# Check that the element order was changed
	print_elements_of(c)

func change(a):
	a[0] = 1

func print_elements_of(array):
	# Here we are using one of the Pool array types
	print(" ".join(PackedStringArray(array)))

# Declare an empty dictionary object
var game = {}

func dict_demo():
	# Initialize a player dictionary
	var player = {
		"name": "Thor",
		"inventory": ["sword", "shield", "map"],
		"location": "Castellion",
		"energy": 67
	}
	
	if game.is_empty():
		# Add data to the game dictionary
		game["player"] = player
		game["score"] = 0
		game["dummy"] = null
	
	if game.has("dummy"):
		game.erase("dummy")
	
	print(game.get("dummy", "Key not found!"))
	
	if game.has_all(["player", "score"]):
		print(game["player"]["name"])
	
	player["energy"] += 1
	
	print(game.keys().size())
	print(game.size())
	print(player.values()[0])
	
	# Alternative way to initialize a dictionary
	var d = {
		a = {
			a1 = {
				a11 = 1, a12 = 2
			},
			a2 = 3
		},
		b = 1
	}
	
	# Make copies of the dictionary
	var deep_copy = d.duplicate(true)
	var shallow_copy = d.duplicate()
	print(deep_copy)
	# I expected the shallow copy to be truncated
	print(shallow_copy)
